﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using Newtonsoft.Json.Linq;
using TianYu.Core.Common;
using TianYu.Core.Pdf.ContentElement;
using System;
using System.Collections.Generic;
using System.Linq;

namespace TianYu.Core.Pdf.Element.Table
{
    public class TableElement : AbstractElement
    {
        /// <summary> 字段 </summary>
        public List<ColumnField> FieldList { get; set; }

        /// <summary> 表头格式 </summary>
        public TableHeaderStyle HeaderStyle { get; set; }

        /// <summary> 内容格式 </summary>
        public TableContentStyle ContentStyle { get; set; }

        /// <summary> 脚注格式 </summary>
        public TableFooterStyle FooterStyle { get; set; }

        public override void Draw(PdfTemplateContent pdfTemplateContent)
        {
            if (FieldList == null || !FieldList.Any()) return;

            var table = new PdfPTable(FieldList.Count);
            //table.WidthPercentage = 100;
            table.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
            table.SplitLate = false;
            table.SplitRows = false;
            //设置每列宽度
            table.SetTotalWidth(FieldList.Select(x => x.ColumnWidth).ToArray());

            //设置表头所在行
            table.HeaderRows = 1;

            //表头
            SetTableHeader(table);

            //内容
            SetTableContent(table, pdfTemplateContent);

            //脚注
            SetTableFooter(table, pdfTemplateContent);

            //设置表格位置 
            table.WriteSelectedRows(0, -1, X, Y + Height, pdfTemplateContent.PdfWriter.DirectContent);

            //pdfTemplateContent.Document.Add(table);
        }


        private void SetTableHeader(PdfPTable table)
        {
            if (!HeaderStyle.IsShow) return;

            foreach (var item in FieldList)
            {
                table.AddCell(CreateCell(HeaderStyle, item.ColumnName, 0, 0));
            }
        }

        private void SetTableContent(PdfPTable table, PdfTemplateContent pdfTemplateContent)
        {
            var text = string.Empty;
            foreach (var rowData in pdfTemplateContent.PdfObject["BoxInvoiceInfo"])
            {
                foreach (var fieldTemp in FieldList)
                {
                    var dataFrom = JObject.FromObject(rowData);

                    text = dataFrom.StringValue(fieldTemp.Field);//取值   

                    table.AddCell(CreateCell(ContentStyle, text, 0, 0));
                }
            }
        }

        private void SetTableFooter(PdfPTable table, PdfTemplateContent pdfTemplateContent)
        {
            if (!FooterStyle.IsShow) return;

            var dataFrom = JObject.FromObject(pdfTemplateContent.PdfObject);

            var text = dataFrom.StringValue("TotalAmount");//取值  

            table.AddCell(CreateCell(FooterStyle, text, FooterStyle.RowHeight, FieldList.Count));
        }

        private PdfPCell CreateCell(FontModel fontModel, string text, float fixedHeight, int colspan)
        {
            var font = new Font(CommonHelper.GetFontFamilyPath(fontModel.Family), fontModel.Size, fontModel.Style, BaseColor.BLACK);

            var pCell = new PdfPCell(new Phrase(text, font));
            if (fixedHeight > 0)
            {
                pCell.FixedHeight = fixedHeight;
            }
             
            pCell.HorizontalAlignment = CommonHelper.GetAlignment(fontModel.Align ?? "center");
            pCell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
            pCell.Colspan = colspan;

            return pCell;
        }
    }
}
